;;(load "err-ut.scm")
;;(load "aux.scm")
(def/err (cy-eval s)
  (let ((l (cyl->scm-l (cadr (cyl s 0))))
	(res #f))
    (if (and (pair? l)(atom? (car l))(symbol? (car l)))
	(eval l)
	(begin
	  (tr-list (i e l 0)
		   (set! res (eval e)))
	  res))))
(def/err (cyl->scm-l l)
  (cond
   ((null? l)
    '())
   ((pair? l)
    (let ((a (nth 0 l))
	  (b (nth 1 l))
	  (c (nth 3 l))
	  )
      ;;(y-or-n "cyl->scm:a=~a,b=~a,c=~a.\n" a b c)
     
      (cond
       ((and (atom? a)(st-is c '无需处理符号))
	'|not handle|)
       ((and (atom? a)(string? a))
	(run/err str->sym-val-l l))
       ((and (atom? a)(not (string? a)))
	(list 'err (nth 4 l) 'cyl->scm (format "开发错误，不允许出现非字符串首项[~a]。" l)))
       ((and (pair? a)(integer? b))
	(run/err list->syn-val-l l))
       ((and (pair? a)(null? b))
	(run/err cyl->scm-l a))
       ((and (pair? a)(pair? b))
	(let ((scm-l '()))
	(tr-list (i e l 0)
		 (let ((la (run/err cyl->scm-l e)))
		   (if (not (eq? la '|not handle|))
		       (push la scm-l))))
	(reverse scm-l)
	))
       (else (list 'err (nth 4 l) 'cyl->scm (format "开发错误，状态未处理。[~a][~a][~a]" a b c)))
       )))
   (else (list 'err (nth 4 l) 'cyl->scm (format "开发错误，不允许是原子数据。[~a]" l)))))
(def/err (str->sym-val-l l)
  (let ((name (nth 0 l))
	(len (nth 1 l))
	(coding (nth 2 l))
	(typ (nth 3 l))
	(ser (nth 4 l))
	(fn (nth 5 l))
	(par-num (nth 6 l))
	)
    (cond
     ((st-is typ '常量)
      (cond
       ((or (eq? typ '整数)(eq? typ '小数)(eq? typ '指数数)(eq? typ '分数))
	(string->number name))
       ((eq? typ '二进制常数)
	(string->number (substring name 2 (string-length name)) 2))
       ((eq? typ '八进制常数)
	(string->number (substring name 2 (string-length name)) 8))
       ((eq? typ '十六进制常数)
	(string->number (substring name 2 (string-length name)) 16))
       ((eq? typ '十进制常数)
	(if (char=? (string-ref name 1) #\d)
	    (string->number (substring name 2 (string-length name)))
	    (string->number (substring name 1 (string-length name)))))
       ((eq? typ '字符常量)
	(cond
	 ((string=? name "#\\空格") (set! name "#\\space"))
	 ((string=? name "#\\回车") (set! name "#\\newline"))
	 ((string=? name "#\\制表符") (set! name "#\\tab")))
	(let ((p (open-input-string name)))
	  (read p)))
       ((eq? typ '字符串)
	(substring name 1 (- (string-length name) 1)))
       ((eq? typ '字符串常量)
	(substring name 2 (- (string-length name) 1)))
       ((eq? typ '扩展常量)
	(list 'quote (string->symbol (substring name 1 (string-length name)))))
       ((eq? typ '列表常量)
	;;not defined
	(list 'err ser 'str->sym-val (format "[~a]尚未定义的常量类型。" typ))
	)
       (else (list 'err ser 'str->sym-val (format "[~a]不是合法的可转换常量类型。" typ))))
      )
     ((st-is typ '变量)
      (string->symbol name))
     ((st-is typ '无参函数)
      (list (string->symbol name)))
     ((eq? typ 'lisp系统函数)
      (string->symbol name))
     ((or (eq? typ '系统宏)(eq? typ '系统关键字))
      (string->symbol name))
     ;;add new type handle part here
     (else (list 'err ser 'str->sym-val (format "[~a]不是预期的原子类型。" typ)))
     )))
(def/err (list->syn-val-l l)
  (let ((item (nth 0 l))
	(len (nth 1 l))
	(coding (nth 2 l))
	(typ (nth 3 l))
	(ser (nth 4 l))
	(fn (nth 5 l))
	(par-num (nth 6 l))
	)
    ;;(y-or-n "list->syn-call:item=~a,typ=~a.\n" item typ)
    (cond
     ((eq? typ 'y表达式调用)
      (yexp-call-l item))
     ((eq? typ 'lisp-call) ;;s表达式调用)
      (sexp-call-l item))
     ((eq? typ '系统关键字调用)
      (sys-key-call-l item))
     ((eq? typ '系统宏调用)
      (sys-macro-call-l item))
     ((eq? typ '函数定义)
      (func-define-proc-l item))
     ((eq? typ '函数体定义)
      (func-define-body item))
     ((eq? typ '并列语句)
      (begin-call-l item))
     ((st-is typ '函数名定义)
      (func-define-name-l item))
     (else (list 'err ser 'list->syn-val (format "[~a]不是合法的列表类型。" typ))))))
(def/err (yexp-call-l l)
  (let ((fn "")
	(fn-l '())
	(par-l '())
	(res-l '())
	(typ #f)
	(scm-sign "【*】")
	)
    (tr-list (i e l 0)
	     (set! name (nth 0 e))
	     (set! typ (nth 3 e))
	     (cond
	      ((eq? typ '函数名片段)
	       (push name fn-l))
	      (else
	       (push scm-sign fn-l)
	       (let ((la (run/err cyl->scm-l e)))
		 (if (not (eq? la '|not handle|))
		     (push la par-l))))
	     ;;(y-or-n "yexp-call:fn-l=~a,par-l=~a.\n" fn-l par-l)
	     ))
    (set! res-l (append (list (string->symbol (merge-str 0 fn-l))) (reverse par-l)))
    res-l))
(def/err (sexp-call-l l)
  (let ((sexp-1-typ (nth 3 (nth 0 l)))
	(sexp-l '())
	(n 0)
	)
    ;;(y-or-n "sexp-call:l=~a,sexp-1-typ=~a.\n" l sexp-1-typ)
    (if (eq? sexp-1-typ 'lisp-begin) (set! n 0)(set! n 1))
    (tr-list (i e l n)
	     (set! name (nth 0 e))
	     (set! typ (nth 3 e))
	     (cond
	      ((eq? typ 'lisp-call)
	       (push (sexp-call-l (car e)) sexp-l))
	      ((or (eq? typ 'lisp-begin)(eq? typ 'lisp-end)(eq? typ '分隔符))
	       )
	      (else
	       (let ((la (run/err cyl->scm-l e)))
		 (if (not (eq? la '|not handle|))
		     (push la sexp-l))))
	     ;;(y-or-n "sexp-call:sexp-l=~a.\n" sexp-l)
	     ))
      (cond
       ((eq? sexp-1-typ 'lisp-begin)
	(reverse sexp-l))
       ((eq? sexp-1-typ 'lisp-quote)
	(cond
	 ((= (length sexp-l) 1)
	  (list 'quote (car sexp-l)))
	 ((> (length sexp-l) 1)
	  (list 'quote sexp-l))
	 (else
	  (list 'err (nth 4 (nth 0 l)) 'sexp-call (format "不允许[']后无符号。")))))
       ((eq? sexp-1-typ 'lisp-unquote)
	(cond
	 ((= (length sexp-l) 1)
	  (list 'unquote (car sexp-l)))
	 ((> (length sexp-l) 1)
	  (list 'unquote sexp-l))
	 (else
	  (list 'err (nth 4 (nth 0 l)) 'sexp-call (format "不允许[,]后无符号。")))))
       ((eq? sexp-1-typ 'lisp-backquote)
	(cond
	 ((= (length sexp-l) 1)
	  (list 'quasiquote (car sexp-l)))
	 ((> (length sexp-l) 1)
	  (list 'quasiquote sexp-l))
	 (else
	  (list 'err (nth 4 (nth 0 l)) 'sexp-call (format "不允许[`]后无s表达式。")))))
       (else
	(list 'err (nth 4 (nth 0 1)) 'sexp-call (format "lisp-call类型cyl首原子符号只允许[`,'(]，不允许是[~a] 。" sexp-1-typ))))))
(def/err (begin-call-l l)
  (let ((proc-l (list '并列语句))
	)
    (tr-list (i e l 0)
	     (let ((la (run/err cyl->scm-l e)))
	       (if (not (eq? la '|not handle|))
		   (push la proc-l))))
    (reverse proc-l)))
  


  
   
	      

	
